package com.wrs.examsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.xml.internal.bind.v2.TODO;
import com.wrs.examsystem.entity.Answer;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.bean.CorrectBean;
import com.wrs.examsystem.service.AnswerService;
import com.wrs.examsystem.service.TestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/examsystem/answer")
public class AnswerController {

    @Resource
    AnswerService answerService;

    @Resource
    TestService testService;


    /**
     * 批改试卷
     * @param correctBean
     * @return
     */
    @PostMapping("correct")
    public R correct(@RequestBody CorrectBean correctBean){
        if (correctBean.getAnswerMark().matches("^\\d{1,}$")){
            return R.error().message("至少为一位数字");
        }
        return answerService.correct(correctBean);
    }

    /**
     * 提交试卷
     * @param answerList
     * @return
     */
    @PostMapping("submitTest")
    public R submitTest(@RequestBody List<Answer> answerList){
//        检查试卷ID是否相同
        int testID = answerList.get(0).getTestId();
        for (int i = 0; i < answerList.size(); i++) {
            if (testID != answerList.get(i).getTestId())
                return R.error().message("第"+i+"题的试卷ID与本题之前的试卷ID不同");
        }
        R test = testService.findTest(testID);
        if (test.getCode() == 20001) return R.error().message("本试卷不存在");
        return answerService.submitTest(answerList);
    }
}

